/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.web.common.environment.checker;

import com.inspur.edp.web.common.environment.EnvironmentException;
import com.inspur.edp.web.common.utility.CommandLineUtility;
import com.inspur.edp.web.common.utility.OperatingSystemUtility;
import com.inspur.edp.web.common.utility.StringUtility;
import io.iec.edp.caf.commons.exception.ExceptionLevel;

/**
 * @Title: EnvironmentChecker  运行环境检测
 * @Description: com.inspur.edp.web.common.environment
 * @Author: Noah
 * @Version: V1.0
 * @Create: 2022/7/29 15:20
 */
public class ExecuteEnvironmentChecker {
    private static final String endMessage = "若已安装，请尝试重启IGIX服务。";

    private static final String prefixMessage = "当前环境";
    /**
     * 默认的弹窗标题
     */
    private static final String defaultModalTitle = "提示";
    /**
     * 默认的弹窗级别
     */
    private static final ExceptionLevel defaultModalLevel = ExceptionLevel.Warning;

    /**
     * 根据不同系统版本返回对应的提示url地址
     *
     * @return
     */
    private static String getKnowledgeUrl() {
        if (OperatingSystemUtility.isLinux()) {
            return "https://open.inspuronline.com/iGIX/#/document/mddoc/igix_2110%2Fdev-guide-beta%2Fquickstart%2Fbuilding-development-environment%2F%E5%BC%80%E5%8F%91%E7%8E%AF%E5%A2%83Linux%E6%90%AD%E5%BB%BA.md";
        }
        return "https://open.inspuronline.com/iGIX/#/document/mddoc/igix_2110%2Fdev-guide-beta%2Fquickstart%2Fbuilding-development-environment%2F%E5%BC%80%E5%8F%91%E7%8E%AF%E5%A2%83Windows%E6%90%AD%E5%BB%BA.md";
    }

    /**
     * 检测是否部署node命令
     *
     * @return
     */
    public static ExecuteEnvironmentCheckResult checkGlobalNodeInstalled() {
        ExecuteEnvironmentCheckResult checkResult = ExecuteEnvironmentCheckResult.getInstance();
        String errorMessage;
        try {
            errorMessage = CommandLineUtility.runCommandWithoutThrows("node -v");
        } catch (RuntimeException ex) {
            errorMessage = ex.getMessage();
        }

        if (StringUtility.isNullOrEmpty(errorMessage)) {
            return checkResult;
        }
        checkResult.setSuccess(false);
        checkResult.setErrorMessage(prefixMessage + "node命令不可用，请安装Nodejs，请参考：<a href='" + getKnowledgeUrl() + "'>Nodejs安装</a>。 " + endMessage);
        return checkResult;
    }

    /**
     * 检测是否部署npm命令
     *
     * @return
     */
    public static ExecuteEnvironmentCheckResult checkGlobalNpmInstalled() {
        ExecuteEnvironmentCheckResult checkResult = ExecuteEnvironmentCheckResult.getInstance();
        String errorMessage;
        try {
            errorMessage = CommandLineUtility.runCommandWithoutThrows("npm -v");
        } catch (RuntimeException ex) {
            errorMessage = ex.getMessage();
        }

        if (StringUtility.isNullOrEmpty(errorMessage)) {
            return checkResult;
        }
        checkResult.setSuccess(false);
        checkResult.setErrorMessage(String.format(prefixMessage + "npm命令不可用，请安装Nodejs，请参考：<a href='" + getKnowledgeUrl() + "'>Nodejs安装</a>。%s ", endMessage));
        return checkResult;
    }

    /**
     * 检测是否部署jit命令
     *
     * @return
     */
    public static ExecuteEnvironmentCheckResult checkGlobalJitEngineInstalled() {
        ExecuteEnvironmentCheckResult checkResult = ExecuteEnvironmentCheckResult.getInstance();
        String errorMessage;
        try {
            errorMessage = CommandLineUtility.runCommandWithoutThrows("jit --version ");
        } catch (RuntimeException ex) {
            errorMessage = ex.getMessage();
        }

        if (StringUtility.isNullOrEmpty(errorMessage)) {
            return checkResult;
        }
        checkResult.setSuccess(false);
        checkResult.setErrorMessage(prefixMessage + "Jit-Engine未部署,请先部署。请参考：<a target='_blank' href='" + getKnowledgeUrl() + "'>Jit-Engine安装</a>。" + endMessage);
        return checkResult;
    }

    /**
     * 检测是否不是ng命令
     *
     * @return
     */
    public static ExecuteEnvironmentCheckResult checkGlobalNgInstalled() {
        ExecuteEnvironmentCheckResult checkResult = ExecuteEnvironmentCheckResult.getInstance();
        String errorMessage;
        try {
            errorMessage = CommandLineUtility.runCommandWithoutThrows("ng --version");
        } catch (RuntimeException ex) {
            errorMessage = ex.getMessage();
        }
        if (StringUtility.isNullOrEmpty(errorMessage)) {
            return checkResult;
        }
        checkResult.setSuccess(false);
        checkResult.setErrorMessage(prefixMessage + "未部署依赖ng命令，请先部署，请参考：" + "<a target='_blank' href='" + getKnowledgeUrl() + "'>Angular-Cli安装</a>。" + endMessage);
        return checkResult;
    }

    /**
     * 编译前环境检查
     */
    public static void beforeCompile() {
        /// 不考虑npm使用安装盘的情况
        ExecuteEnvironmentCheckResult nodeCheckResult = ExecuteEnvironmentChecker.checkGlobalNodeInstalled();
        if (!nodeCheckResult.isSuccess()) {
            throw new EnvironmentException(nodeCheckResult.getErrorMessage(), defaultModalTitle, defaultModalLevel);
        }
        ExecuteEnvironmentCheckResult npmCheckResult = ExecuteEnvironmentChecker.checkGlobalNpmInstalled();
        if (!npmCheckResult.isSuccess()) {
            throw new EnvironmentException(npmCheckResult.getErrorMessage(), defaultModalTitle, defaultModalLevel);
        }

        ExecuteEnvironmentCheckResult ngCheckResult = ExecuteEnvironmentChecker.checkGlobalNgInstalled();
        if (!ngCheckResult.isSuccess()) {
            throw new EnvironmentException(ngCheckResult.getErrorMessage(), defaultModalTitle, defaultModalLevel);
        }
    }

    /**
     * 生成前环境检查
     */
    public static void beforeGenerate() {
        /// 不考虑npm使用安装盘的情况
        ExecuteEnvironmentCheckResult nodeCheckResult = ExecuteEnvironmentChecker.checkGlobalNodeInstalled();
        if (!nodeCheckResult.isSuccess()) {
            throw new EnvironmentException(nodeCheckResult.getErrorMessage(), defaultModalTitle, defaultModalLevel);
        }

        ExecuteEnvironmentCheckResult jitCheckResult = ExecuteEnvironmentChecker.checkGlobalJitEngineInstalled();
        if (!jitCheckResult.isSuccess()) {
            throw new EnvironmentException(jitCheckResult.getErrorMessage(), defaultModalTitle, defaultModalLevel);
        }
    }
}
